package com.easyadmin.pro.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.easyadmin.pro.common.constants.StatusConstants;
import com.easyadmin.pro.common.enums.HttpCodeEnum;
import com.easyadmin.pro.common.exception.BusinessException;
import com.easyadmin.pro.modules.sys.vo.RelevanceActionInfoVo;
import com.easyadmin.pro.tool.WebUtils;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.easyadmin.pro.modules.sys.entity.RouterActionEntity;
import com.easyadmin.pro.modules.sys.mapper.RouterActionMapper;
import com.easyadmin.pro.modules.sys.service.IRouterActionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *  服务层实现。
 *
 * @author xulin
 * @since 2024-04-07
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RouterActionServiceImpl extends ServiceImpl<RouterActionMapper, RouterActionEntity> implements IRouterActionService {

    private final RouterActionMapper routerActionMapper;

    @Override
    public void saveRelevanceActionInfo(RelevanceActionInfoVo relevanceActionInfoVo) {
        Long routerId = relevanceActionInfoVo.getRouterId();
        if (Objects.isNull(routerId)) {
            throw new BusinessException(HttpCodeEnum.ROUTER_ID_NOT_FOUND);
        }

        List<Long> actionInfo = relevanceActionInfoVo.getActionInfo();
        if (CollectionUtil.isEmpty(actionInfo)) {
            UpdateChain.of(RouterActionEntity.class)
                    .set(RouterActionEntity::getIsDeleted, StatusConstants.STATUS_1)
                    .set(RouterActionEntity::getUpdateUserId, WebUtils.getUserId())
                    .set(RouterActionEntity::getUpdateTime, LocalDateTime.now())
                    .eq(RouterActionEntity::getRouterId, routerId).update();
        } else {
            UpdateChain.of(RouterActionEntity.class)
                    .set(RouterActionEntity::getIsDeleted, StatusConstants.STATUS_1)
                    .set(RouterActionEntity::getUpdateUserId, WebUtils.getUserId())
                    .set(RouterActionEntity::getUpdateTime, LocalDateTime.now())
                    .eq(RouterActionEntity::getRouterId, routerId)
                    .notIn(RouterActionEntity::getActionId, actionInfo).update();
            List<RouterActionEntity> saveInfos = new ArrayList<>();
            List<Long> sourceActionInfos = this.listAs(new QueryWrapper().select(RouterActionEntity::getActionId)
                    .eq(RouterActionEntity::getRouterId, routerId), Long.class);
            actionInfo.forEach(e -> {
                if (!sourceActionInfos.contains(e)) {
                    RouterActionEntity routerActionEntity = new RouterActionEntity();
                    routerActionEntity.setRouterId(routerId).setActionId(e);
                    saveInfos.add(routerActionEntity);
                }
            });
            if (CollectionUtil.isNotEmpty(saveInfos)) {
                this.saveOrUpdateBatch(saveInfos);
            }
        }
    }
}
